package dk.eclipse.android.helpers;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;

/**
 * @author Ronni
 * 
 */
public class SoundHelper {

	private static HashMap<Integer, Integer> soundIds = new HashMap<Integer, Integer>();
	private static SoundPool soundPool = new SoundPool(100,
			AudioManager.STREAM_MUSIC, 0);

	static {
		soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

			@Override
			public void onLoadComplete(SoundPool soundPool, int sampleId,
					int status) {

				soundPool.play(sampleId, 1.0f, 1.0f, 1, 0, 1.0f);

			}
		});
	}

	/**
	 * @param context
	 * @param resourceId
	 * @param looping
	 */
	public static void playSound(Context context, int resourceId,
			boolean looping) {
		if (soundIds.containsKey(resourceId)) {
			soundPool.play(soundIds.get(resourceId), 1.0f, 1.0f, 1, 0, 1.0f);
		} else {
			// A new sound. Let's load it and let the load listener play the
			// sound.
			soundIds.put(resourceId, soundPool.load(context, resourceId, 1));
		}

	}

	/**
	 * @param context
	 * @param resourceId
	 */
	public static void stopSound(Context context, int resourceId) {
		int soundId = 0;
		if (!soundIds.containsKey(resourceId)) {
			return;
		} else {
			soundId = soundIds.get(resourceId);
		}
		soundPool.stop(soundId);

	}

	/**
	 * @return the soundPool
	 */
	public static SoundPool getSoundPool() {
		return soundPool;
	}

}
